<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-select v-model="listQuery.status" clearable style="width: 200px" class="filter-item" placeholder="请选择状态" >
        <el-option v-for="(item,index) in statusMap" :key="index" :label="item.name" :value="item.value" />
      </el-select>
      <el-button
        v-permission="['warehouse:purchasewarehouseordergoods:query']"
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >查找</el-button>
      <el-button
        v-permission="['warehouse:purchasewarehouseordergoods:create']"
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >添加</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      size="small"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
      style="white-space: pre-line"
    >
      <el-table-column prop="id" align="center" label=""/>
      <el-table-column prop="warehouseOrderId" align="center" label="入库单id"/>
      <el-table-column prop="warehouseId" align="center" label=""/>
      <el-table-column prop="warehouseGoodsBuyNum" align="center" label="数量"/>
      <el-table-column prop="warehouseGoodsPrice" align="center" label="单价"/>
      <el-table-column prop="warehouseGoodsTax" align="center" label="税"/>
      <el-table-column prop="warehouseGoodsAmount" align="center" label="商品价格"/>
      <el-table-column prop="goodsId" align="center" label="（对应使用sku的信息）"/>
      <el-table-column prop="goodsName" align="center" label=""/>
      <el-table-column prop="goodsNo" align="center" label="商品编码"/>
      <el-table-column prop="goodsSpec" align="center" label="商品规格"/>
      <el-table-column prop="goodsUnit" align="center" label=""/>
      <el-table-column align="center" label="操作" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-permission="['warehouse:purchasewarehouseordergoods:update']"
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
            v-permission="['warehouse:purchasewarehouseordergoods:update']"
            type="info"
            size="mini"
            @click="handleDetail(scope.row)"
          >查看</el-button>
          <el-button
            v-permission="['warehouse:purchasewarehouseordergoods:delete']"
            type="danger"
            size="mini"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="dataForm"
        status-icon
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="" prop="id">
          <el-input v-model="dataForm.id" disabled />
        </el-form-item>
        <el-form-item label="入库单id" prop="warehouseOrderId">
          <el-input v-model="dataForm.warehouseOrderId" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="" prop="warehouseId">
          <el-input v-model="dataForm.warehouseId" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="数量" prop="warehouseGoodsBuyNum">
          <el-input v-model="dataForm.warehouseGoodsBuyNum" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="单价" prop="warehouseGoodsPrice">
          <el-input v-model="dataForm.warehouseGoodsPrice" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="税" prop="warehouseGoodsTax">
          <el-input v-model="dataForm.warehouseGoodsTax" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="商品价格" prop="warehouseGoodsAmount">
          <el-input v-model="dataForm.warehouseGoodsAmount" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="（对应使用sku的信息）" prop="goodsId">
          <el-input v-model="dataForm.goodsId" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="" prop="goodsName">
          <el-input v-model="dataForm.goodsName" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="商品编码" prop="goodsNo">
          <el-input v-model="dataForm.goodsNo" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="商品规格" prop="goodsSpec">
          <el-input v-model="dataForm.goodsSpec" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="" prop="goodsUnit">
          <el-input v-model="dataForm.goodsUnit" clearable placeholder=""/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
        <el-button v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
.el-table .cell {
  white-space: pre-line;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>

<script>
import { list, create, del, update, active } from '@/api/purchasewarehouseordergoods'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
export default {
  name: 'PurchaseWarehouseOrderGoods',
  components: { Pagination },
  filters: {
  },
  data() {
    return {
      statusMap: [{ value: 0, name: '禁用' }, { value: 1, name: '正常' }],
      list: undefined,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        limit: 20,
        status: undefined
      },
      options: [],
      dataForm: {
        id: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      list(this.listQuery)
        .then(response => {
          this.list = response.data.data.items
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined
      }
    },
    handleStatus(row) {
      this.resetForm()
      this.dataForm.id = row.id
      this.dataForm.status = row.status === 1 ? 0 : 1
      active(this.dataForm)
        .then(res => {
          this.$notify.success({
            title: '成功',
            message: '状态更改成功'
          })
          this.getList()
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      create(this.dataForm)
        .then(response => {
          this.getList()
          this.dialogFormVisible = false
          this.$notify.success({
            title: '成功',
            message: '创建成功'
          })
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: '失败'
          })
        })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      update(this.dataForm)
        .then(response => {
          this.getList()
          this.dialogFormVisible = false
          this.$notify.success({
            title: '成功',
            message: '更新成功'
          })
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: '失败'
          })
        })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: '失败'
            })
          })
      }).catch(() => {
        return false
      })
    }
  }
}
</script>

